home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4838 / 4838.xpi / chrome / multipletab.jar / content / multipletab / lib / arrowScrollBoxScrollHelper.js next >
Text File  |  2010-02-03  |  4KB  |  139 lines

  1. /*
  2.  ArrowScrollBox Scroll Helper Library
  3.  
  4.  Usage:
  5.    XUL:
  6.      <arrowscrollbox id="scrollbox" orient="vertical">
  7.        <vbox>
  8.          <checkbox/>
  9.          <checkbox/>
  10.          <checkbox/>
  11.          <checkbox/>
  12.        </vbox>
  13.      </arrowscrollbox>
  14.  
  15.   JavaScript:
  16.      var box = document.getElementById('scrollbox');
  17.      var base = box.getElementsByTagName('checkbox')[0];
  18.      new window['piro.sakura.ne.jp'].arrowScrollBoxScrollHelper(box, base);
  19.      // 1st argument: DOMNode or ID of an element
  20.      // 2nd argument: DOMNode, ID of an element, or local name of a child element
  21.  
  22.  lisence: The MIT License, Copyright (c) 2009 SHIMODA "Piro" Hiroshi
  23.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
  24.  original:
  25.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/arrowScrollBoxScrollHelper.js
  26. */
  27. (function() {
  28.     const currentRevision = 1;
  29.  
  30.     if (!('piro.sakura.ne.jp' in window)) window['piro.sakura.ne.jp'] = {};
  31.  
  32.     var loadedRevision = 'arrowScrollBoxScrollHelper' in window['piro.sakura.ne.jp'] ?
  33.             window['piro.sakura.ne.jp'].arrowScrollBoxScrollHelper.prototype.revision :
  34.             0 ;
  35.     if (loadedRevision && loadedRevision > currentRevision) {
  36.         return;
  37.     }
  38.  
  39.     var Cc = Components.classes;
  40.     var Ci = Components.interfaces;
  41.  
  42.     window['piro.sakura.ne.jp'].arrowScrollBoxScrollHelper = function(aArrowScrollBox, aBase)
  43.     {
  44.         this._initArrowScrollBox(aArrowScrollBox);
  45.         this._initBase(aBase);
  46.  
  47.         this._box.addEventListener('DOMMouseScroll', this, true);
  48.         window.addEventListener('unload', this, false);
  49.     };
  50.     window['piro.sakura.ne.jp'].arrowScrollBoxScrollHelper.prototype = {
  51.  
  52.         revision : currentRevision,
  53.  
  54.         kBASE_PIXELS : 10,
  55.  
  56.         _initArrowScrollBox : function(aArrowScrollBox)
  57.         {
  58.             this._box = aArrowScrollBox;
  59.             if (typeof this._box == 'string')
  60.                 this._box = document.getElementById(this._box);
  61.  
  62.             if (
  63.                 !this._box ||
  64.                 !(this._box instanceof Ci.nsIDOMElement) ||
  65.                 this._box.localName != 'arrowscrollbox'
  66.                 )
  67.                 throw new Error('arrowscrollbox is required!');
  68.         },
  69.  
  70.         _initBase : function(aBase)
  71.         {
  72.             this._base = aBase;
  73.             if (typeof this._base == 'string') {
  74.                 var num = Number(this._base);
  75.                 if (!isNaN(num)) {
  76.                     this._base = num;
  77.                 }
  78.                 else {
  79.                     var node = document.getElementById(this._base);
  80.                     if (node) {
  81.                         this._base = node;
  82.                     }
  83.                     else {
  84.                         node = this._box.getElementsByTagName(this._base);
  85.                         if (node && node.length) {
  86.                             this._base = node[0];
  87.                         }
  88.                         else {
  89.                             this._base = null;
  90.                         }
  91.                     }
  92.                 }
  93.             }
  94.         },
  95.  
  96.         handleEvent : function(aEvent)
  97.         {
  98.             switch (aEvent.type)
  99.             {
  100.                 case 'unload':
  101.                     this._box.removeEventListener('DOMMouseScroll', this, true);
  102.                     window.removeEventListener('unload', this, false);
  103.                     delete this._base;
  104.                     delete this._box;
  105.                     return;
  106.  
  107.                 case 'DOMMouseScroll':
  108.                     this._onScroll(aEvent);
  109.                     break;
  110.             }
  111.         },
  112.  
  113.         _onScroll : function(aEvent)
  114.         {
  115.             if ('axis' in aEvent &&
  116.                 aEvent.axis == aEvent.HORIZONTAL_AXIS &&
  117.                 this._box.orient == 'vertical')
  118.                 return;
  119.  
  120.             var baseBox, base;
  121.             if (this._base) {
  122.                 if (typeof this._base == 'number')
  123.                     base = this._base;
  124.                 else if (this._base instanceof Ci.nsIBoxObject)
  125.                     baseBox = this._base;
  126.                 else if (this._base instanceof Ci.nsIDOMElement)
  127.                     baseBox = this._base.boxObject;
  128.  
  129.                 if (baseBox)
  130.                     base = baseBox[this._box.orient == 'vertical' ? 'height' : 'width' ];
  131.             }
  132.             if (!base) base = this.kBASE_PIXELS;
  133.  
  134.             this._box.scrollByPixels(base * aEvent.detail);
  135.             aEvent.stopPropagation();
  136.         }
  137.     };
  138. })();
  139.